package com.chinafree.auth.service.impl;

import com.chinafree.auth.model.enumeration.VerficationEnum;
import com.chinafree.auth.model.param.LoginParam;
import com.chinafree.auth.model.result.LoginResult;
import com.chinafree.auth.service.LoginService;
import com.chinafree.auth.service.LoginUserService;
import com.chinafree.auth.service.NormalLoginService;
import com.chinafree.auth.service.VerificationCode;
import com.chinafree.common.model.enumeration.ResponseCodeEnum;
import com.chinafree.common.model.response.BaseResponse;
import com.chinafree.common.model.response.EntityResponse;
import com.chinafree.mapper.SysLoginLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private NormalLoginService normalLoginService;
    @Autowired
    private VerificationCode verificationCode;
    @Autowired
    private LoginUserService loginUserService;
    @Autowired
    private SysLoginLogMapper sysLoginLogMapper;

    @Override
    public EntityResponse<LoginResult> loginByUsernameAndPwd(LoginParam loginParam) {
        String loginName = loginParam.getUsername();
        String password = loginParam.getPassword();
        Integer roleId = loginParam.getRoleId();
        if (loginName == null || password == null) {
            return new EntityResponse<>(ResponseCodeEnum.FORBIDDEN, "手机或密码没填写", null);
        }
        EntityResponse<LoginResult> result = normalLoginService.loginByUserNameAndPassword(loginName, password, roleId);
        return result;
    }

    @Override
    public EntityResponse<LoginResult> LoginByPhoneAndVerfication(LoginParam loginParam) {
        String loginName = loginParam.getUsername();
        String verfication = loginParam.getPassword();
        Integer roleId = loginParam.getRoleId();
        if (loginName == null || verfication == null) {
            return new EntityResponse<>(ResponseCodeEnum.FORBIDDEN, "手机或验证码没填写", null);
        }
        return normalLoginService.loginByUserNameAndVerfication(loginName, verfication, roleId);
    }

    @Override
    public BaseResponse sendRegisterVerificationCode(String phone) {
        return verificationCode.sendRegisterVerificationCode(VerficationEnum.LOGIN.getName(), phone);
    }


}